package com.cjm.lecode;

import java.util.HashMap;
import java.util.Map;

public class TwoNumHe {
    public static int[] twoSum(int[] numbers, int target) {
        return method2(numbers, target);
    }
    private static int[] method2(int[] numbers, int target) {
        int i = 0;
        int j = numbers.length - 1;

        while (i <= j) {
            int value = numbers[i] + numbers[j];
            if (target == value) {
                return new int[]{i + 1, j + 1};
            }
            if (target < value) {
                j--;
            }
            if (target > value) {
                i++;
            }
        }


        return null;
    }
    private static int[] method1(int[] numbers, int target) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < numbers.length; i++) {
            int number = numbers[i];
            int value = target - number;
            Integer integer = map.get(value);
            if (integer != null && integer != i) {
                return new int[]{integer + 1, i+1};
            }
            map.put(number, i);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] one = twoSum(new int[]{2, 7, 11, 15}, 9);
        int[] two = twoSum(new int[]{2, 3, 4}, 6);
        int[] ints = twoSum(new int[]{-1, 0}, -1);
        System.err.println("OK");
    }
}
